/*
 * Copyright 2013 Sigurd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.digiway.rapidbreeze.client.model.collector;

import de.digiway.rapidbreeze.client.infrastructure.updatable.UpdatableEntry;
import de.digiway.rapidbreeze.shared.rest.url.UrlResource;
import java.net.URL;
import java.util.Objects;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.LongProperty;
import javafx.beans.property.Property;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleLongProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import org.apache.commons.lang3.ObjectUtils;

/**
 *
 * @author Sigurd
 */
public class CollectorModel implements UpdatableEntry<UrlResource> {

    public static final String PROP_FILENAME = "filename";
    public static final String PROP_CLOUD_PROVIDER = "cloudProvider";
    public static final String PROP_FILESIZE = "filesize";
    public static final String PROP_STATUS = "status";
    public static final String PROP_URL = "url";
    private StringProperty filename = new SimpleStringProperty();
    private StringProperty cloudProvider = new SimpleStringProperty();
    private LongProperty filesize = new SimpleLongProperty();
    private StringProperty status = new SimpleStringProperty();
    private StringProperty url = new SimpleStringProperty();
    private BooleanProperty available = new SimpleBooleanProperty();

    public CollectorModel(UrlResource resource) {
        this.url.setValue(resource.getUrl());
        update(resource);
    }

    public CollectorModel(URL url) {
        this.url.setValue(url.toString());
    }

    @Override
    public final void update(UrlResource resource) {
        if (!ObjectUtils.equals(getFilename(), resource.getFilename())) {
            this.filename.setValue(resource.getFilename());
        }
        if (!ObjectUtils.equals(getCloudProvider(), resource.getCloudProvider())) {
            this.cloudProvider.setValue(resource.getCloudProvider());
        }
        if (!ObjectUtils.equals(getFilesize(), resource.getFilesize())) {
            this.filesize.setValue(resource.getFilesize());
        }
        if (!ObjectUtils.equals(getStatus(), resource.getStatus())) {
            this.status.setValue(resource.getStatus());
        }
        if (!ObjectUtils.equals(this.available.getValue(), resource.isAvailable())) {
            this.available.setValue(resource.isAvailable());
        }
    }

    @Override
    public String getIdentifier() {
        return getUrl();
    }

    public String getUrl() {
        return url.getValue();
    }

    public String getFilename() {
        return filename.getValue();
    }

    public void setFilename(String value) {
        filename.setValue(value);
    }

    public boolean isAvailable() {
        return available.getValue();
    }

    public String getCloudProvider() {
        return cloudProvider.getValue();
    }

    public Long getFilesize() {
        return filesize.getValue();
    }

    public String getStatus() {
        return status.getValue();
    }

    public Property filenameProperty() {
        return filename;
    }

    public Property cloudProviderProperty() {
        return cloudProvider;
    }

    public Property filesizeProperty() {
        return filesize;
    }

    public Property statusProperty() {
        return status;
    }

    public Property availableProperty() {
        return available;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 97 * hash + Objects.hashCode(this.url.getValue());
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final CollectorModel other = (CollectorModel) obj;
        if (!Objects.equals(this.url.getValue(), other.url.getValue())) {
            return false;
        }
        return true;
    }
}
